package com.xyht.sca_s.student_manage_system.scheduling;

import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;

@Component
@Log4j2
public class OrderStatusSchedulingTask {
    /**
     * @Autowired 对象定义、组件引用
     */


    @Resource
    PlatformTransactionManager transactionManager;

//    @Scheduled(cron = "0 */1 * * * ?")//每隔 1 分钟执行一次
    public void schedulingTask() {

    }

    private void executeInTransaction(Runnable task) {
        DefaultTransactionDefinition dt = new DefaultTransactionDefinition();
        dt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
        TransactionStatus status = transactionManager.getTransaction(dt);

        try {
            task.run();
            transactionManager.commit(status);
        } catch (Exception e) {
            e.printStackTrace();
            transactionManager.rollback(status);
        } finally {
            if (status.isNewTransaction() && !status.isCompleted()) {
                transactionManager.commit(status);
            }
        }
    }

    //++++++++++++++++++++++++++++++++++++++++++邓翔内+++++++++++++++++++++++++++++++++++++++++

    /**
     * 需要定时查询的逻辑
     */
    public void test1() {
        executeInTransaction(() -> {
            //逻辑代码
        });
    }


}
